using System;
using System.Linq;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;

namespace JGSY.AI.DesktopUI.ViewModels;

public partial class ProfileViewModel : ViewModelBase
{
    [ObservableProperty]
    private string _username = "JGSY用户";

    [ObservableProperty]
    private string _email = "user@jgsy.ai";

    [ObservableProperty]
    private string _occupation = "AI开发者";

    [ObservableProperty]
    private string _usageDays = "7";

    [ObservableProperty]
    private string _launchCount = "25";

    [ObservableProperty]
    private string _lastUsed = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

    public ICommand SaveCommand { get; }
    public ICommand ResetCommand { get; }
    public ICommand CloseCommand { get; }

    public ProfileViewModel()
    {
        SaveCommand = new RelayCommand(OnSave);
        ResetCommand = new RelayCommand(OnReset);
        CloseCommand = new RelayCommand(OnClose);
    }

    private void OnSave()
    {
        // TODO: 保存用户资料到配置文件或数据库
        System.Diagnostics.Debug.WriteLine($"保存用户资料: {Username}, {Email}, {Occupation}");
    }

    private void OnReset()
    {
        Username = "JGSY用户";
        Email = "user@jgsy.ai";
        Occupation = "AI开发者";
    }

    private void OnClose()
    {
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var profileWindow = desktop.Windows.FirstOrDefault(w => w.GetType().Name == "ProfileWindow");
            profileWindow?.Close();
        }
    }
}
